home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Sources / TextFacet.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  10.0 KB  |  386 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                            TextFacet.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                        Anthone Burbidge
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. // ----- TextPart Includes -----
  14.  
  15. #ifndef _TEXTUTIL_
  16. #include "TextUtil.h"
  17. #endif
  18.  
  19. #ifndef _TEXTFACET_
  20. #include "TextFacet.h"
  21. #endif
  22.  
  23. #ifndef _TEXTFRAME_
  24. #include "TextFrame.h"
  25. #endif
  26.  
  27. #ifndef _TEXTPART_
  28. #include "TextPart.h"
  29. #endif
  30.  
  31. #ifndef _RULERFRAME_
  32. #include "RulerFrame.h"
  33. #endif
  34.  
  35. #ifndef _TEXTSELC_
  36. #include "TextSelc.h"
  37. #endif
  38.  
  39. #ifndef _ODTEXT_
  40. #include "ODText.h"
  41. #endif
  42.  
  43. // ----- Textension Includes -----
  44.  
  45. #ifndef _Textension_
  46. #include "Textension.h"
  47. #endif
  48.  
  49. // ----- Framework Includes -----
  50.  
  51. #ifndef FWFRAME_H
  52. #include <FWFrame.h>
  53. #endif
  54.  
  55. #ifndef FWRECT_H
  56. #include <FWRect.h>
  57. #endif
  58.  
  59. #ifndef FWUTIL_H
  60. #include <FWUtil.h>
  61. #endif
  62.  
  63. // ----- OpenDoc Includes -----
  64.  
  65. #ifndef _SHAPE_
  66. #include <Shape.h>
  67. #endif
  68.  
  69. #ifndef _WINDOW_
  70. #include <Window.h>
  71. #endif
  72.  
  73. #ifndef _TRNSFORM_
  74. #include <Trnsform.h>
  75. #endif
  76.  
  77. #ifndef _DRAGDROP_
  78. #include <DragDrop.h>
  79. #endif
  80.  
  81. #ifndef _STDPROPS_
  82. #include <StdProps.h>
  83. #endif
  84.  
  85. #ifndef _STDTYPES_
  86. #include <StdTypes.h>
  87. #endif
  88.  
  89. #ifndef _DGITMITR_
  90. #include <DgItmItr.h>
  91. #endif
  92.  
  93. #ifndef _TRANSLAM_
  94. #include <TranslaM.h>
  95. #endif
  96.  
  97. #ifndef _XMPSESSM_
  98. #include <XMPSessM.h>
  99. #endif
  100.  
  101. // ----- Macintosh Includes -----
  102.  
  103. #ifndef __FIXMATH__
  104. #include <FixMath.h>
  105. #endif
  106.  
  107. #ifndef mathRoutinesIncludes
  108. #include <Math Routines.h>
  109. #endif
  110.  
  111. #ifndef __TOOLUTILS__
  112. #include <ToolUtils.h>
  113. #endif
  114.  
  115. #pragma segment TextPartSegment
  116.  
  117. //========================================================================================
  118. // CLASS CTextFacet
  119. //========================================================================================
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // CTextFacet::CTextFacet
  123. //----------------------------------------------------------------------------------------
  124.  
  125. CTextFacet::CTextFacet() :
  126.     FW_CFacet(),
  127.     fTextPart(NULL)
  128. {
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // CTextFacet::InitTextFacet
  133. //----------------------------------------------------------------------------------------
  134.  
  135. void CTextFacet::InitTextFacet(XMPFacet* facet, CTextPart* textPart)
  136. {
  137.     InitFacet(facet);
  138.     fTextPart = textPart;
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // CTextFacet::~CTextFacet
  143. //----------------------------------------------------------------------------------------
  144.  
  145. CTextFacet::~CTextFacet()
  146. {
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // CTextFacet::Draw
  151. //----------------------------------------------------------------------------------------
  152.  
  153. void CTextFacet::Draw(FW_CGraphicContext *gc)
  154. {
  155.     XMPShape* clipShape = ::NewXMPShape();
  156.     gc->GetClipShape(clipShape);
  157.  
  158.     ::EraseRgn(clipShape->GetQDRegion());
  159.  
  160.     CTextDrawInitiator di(this);
  161.     
  162.     FW_SPlatformRect bounds = (**clipShape->GetQDRegion()).rgnBBox;
  163.     
  164.     fTextPart->SetEmbeddedRunFrame((CTextFrame *) GetFrame());
  165.     fTextPart->SetEmbeddedRunFacet(this);
  166.  
  167.     (fTextPart->GetEditText())->Draw(&bounds, kNoDrawFlags);
  168.     
  169.     delete clipShape;
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // CTextFacet::DoMouseDown
  174. //----------------------------------------------------------------------------------------
  175.  
  176. FW_Boolean CTextFacet::DoMouseDown(const FW_CPoint& where,
  177.                                    XMPEventData event)
  178. {
  179. FW_UNUSED(where);
  180.  
  181.     CTextDrawInitiator di(this);
  182.  
  183.     EventRecord localEvent = *event;
  184.     
  185.     FW_CRect sbBounds;
  186.     
  187.     fTextPart->SetEmbeddedRunFrame((CTextFrame *) GetFrame());
  188.     fTextPart->SetEmbeddedRunFacet(this);
  189.     fTextPart->SetEmbeddedRunEvent(event);
  190.  
  191.     // The following statement delegates the responsibility of dragging to an embedded
  192.     // part if the selection consists solely of the part. This gives us a chance to see
  193.     // if the mouse is in the resize handle.
  194.     
  195.     FW_Boolean didDrag = FALSE;
  196.     if (((CTextSelection *) fTextPart->GetSelection())->IsPointInSelection(where)
  197.         && !((CTextSelection *) fTextPart->GetSelection())->IsSelectionOnlyOneProxyRun())
  198.         didDrag = fTextPart->GetSelection()->Drag(this, event);
  199.     
  200.     if (!didDrag)
  201.     {
  202.         TClickCommandInfo clickCommandInfo;
  203.         (fTextPart->GetEditText())->Click(&localEvent, &clickCommandInfo, NULL, NULL);
  204.     }
  205.     
  206.     return true;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // CTextFacet::MouseEnter
  211. //----------------------------------------------------------------------------------------
  212.  
  213. void CTextFacet::MouseEnter(const FW_CPoint& where)
  214. {
  215. FW_UNUSED(where);
  216.     SetCursor(*GetCursor(iBeamCursor)); 
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // CTextFacet::NewDropTracker
  221. //----------------------------------------------------------------------------------------
  222.  
  223. FW_CDropTracker* CTextFacet::NewDropTracker(FW_CFacet* facet)
  224. {
  225.     CTextDropTracker* dropTracker = new CTextDropTracker;
  226.     dropTracker->InitTextDropTracker((CTextFacet *) facet);
  227.     return dropTracker;
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // CTextFacet::DragEnter
  232. //----------------------------------------------------------------------------------------
  233.  
  234. void CTextFacet::DragEnter(XMPDragItemIterator* dragInfo, const FW_CPoint& where)
  235. {
  236.     FW_CFacet::DragEnter(dragInfo, where);
  237.     
  238.     if (!this->GetCanAcceptDrop())
  239.     {
  240.         XMPStorageUnit *dragSU;
  241.         
  242.         XMPTranslation *translate = GetFrame()->GetPart()->GetSession()->GetTranslation();
  243.         XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  244.  
  245.         for (dragSU = dragInfo->First(); dragSU; dragSU = dragInfo->Next())
  246.             if (dragSU->Exists(kXMPPropContents, kAppleTEXT, 0))
  247.                 this->SetCanAcceptDrop(TRUE);
  248.     }
  249. }
  250.  
  251. //----------------------------------------------------------------------------------------
  252. // CTextFacet::DrawDragHiliteInside
  253. //----------------------------------------------------------------------------------------
  254.  
  255. FW_Boolean CTextFacet::DrawDragHiliteInside()
  256. {
  257.     return FALSE;
  258. }
  259.  
  260.  
  261. //========================================================================================
  262. // CLASS CTextDropTracker
  263. //========================================================================================
  264.  
  265. //----------------------------------------------------------------------------------------
  266. // CTextDropTracker::CTextDropTracker
  267. //----------------------------------------------------------------------------------------
  268.  
  269. CTextDropTracker::CTextDropTracker()
  270. {
  271. }
  272.  
  273. //----------------------------------------------------------------------------------------
  274. // CTextDropTracker::~CTextDropTracker
  275. //----------------------------------------------------------------------------------------
  276.  
  277. CTextDropTracker::~CTextDropTracker()
  278. {
  279. }
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // CTextDropTracker::InitTextDropTracker
  283. //----------------------------------------------------------------------------------------
  284.  
  285. void CTextDropTracker::InitTextDropTracker(CTextFacet* facet)
  286. {
  287.     FW_CDropTracker::InitDropTracker(facet);
  288. }
  289.  
  290. //----------------------------------------------------------------------------------------
  291. // CTextDropTracker::BeginTracking
  292. //----------------------------------------------------------------------------------------
  293.  
  294. void CTextDropTracker::BeginTracking(const FW_CPoint& where)
  295. {
  296. #if 0
  297.     char str[80];
  298.     sprintf(str, (const char *) "\pBeginTracking: where=(%d,%d)\n", FixedToInt(where.x), FixedToInt(where.y));
  299.     SysBreakFunc((unsigned const char *) str);
  300. #endif
  301.     
  302.     fCharOffset = this->GetCharOffset(where);
  303.     this->DrawCaret(fCharOffset);
  304. }
  305.  
  306. //----------------------------------------------------------------------------------------
  307. // CTextDropTracker::ContinueTracking
  308. //----------------------------------------------------------------------------------------
  309.  
  310. void CTextDropTracker::ContinueTracking(const FW_CPoint& where)
  311. {
  312.     TOffset newOffset = this->GetCharOffset(where);
  313.     if (newOffset != fCharOffset)
  314.     {
  315. #if 0
  316.         char str[80];
  317.         sprintf(str, (const char *) "\pContinueTracking: where=(%d,%d)\n", FixedToInt(where.x), FixedToInt(where.y));
  318.         SysBreakFunc((unsigned const char *) str);
  319. #endif
  320.     
  321.         this->DrawCaret(fCharOffset);
  322.         this->DrawCaret(newOffset);
  323.         fCharOffset = newOffset;
  324.     }
  325. }
  326.  
  327. //----------------------------------------------------------------------------------------
  328. // CTextDropTracker::EndTracking
  329. //----------------------------------------------------------------------------------------
  330.  
  331. void CTextDropTracker::EndTracking(const FW_CPoint& where)
  332. {
  333. FW_UNUSED(where);
  334.  
  335. #if 0
  336.     char str[80];
  337.     sprintf(str, (const char *) "\pEndTracking: where=(%d,%d)\n", FixedToInt(where.x), FixedToInt(where.y));
  338.     SysBreakFunc((unsigned const char *) str);
  339. #endif
  340.     
  341.     this->DrawCaret(fCharOffset);
  342. }
  343.  
  344. //----------------------------------------------------------------------------------------
  345. // CTextDropTracker::GetCharOffset
  346. //----------------------------------------------------------------------------------------
  347.  
  348. TOffset CTextDropTracker::GetCharOffset(const FW_CPoint& where) const
  349. {
  350.     TOffsetRange range;
  351.     Boolean outside;
  352.     
  353.     FW_SPlatformPoint pt;
  354.     where.AsPlatformPoint(pt);
  355.     
  356.     this->GetTextPart()->GetEditText()->Point2Char(pt, &range, &outside);
  357.     
  358.     return range.Start();
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // CTextDropTracker::DrawCaret
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void CTextDropTracker::DrawCaret(TOffset charOffset)
  366. {
  367. #if 0
  368.     char str[80];
  369.     sprintf(str, (const char *) "\pDrawCaret: offset=%ld\n", charOffset.offset);
  370.     SysBreakFunc((unsigned const char *) str);
  371. #endif
  372.  
  373.     short hite;
  374.     long top, trailEdge;
  375.     
  376.     this->GetTextPart()->GetEditText()->Char2Point(charOffset, &top, &trailEdge, &hite);
  377.  
  378.     PenMode(patXor);
  379.     
  380.     ::MoveTo((short) trailEdge, (short) top);
  381.     ::Line(0, hite);
  382.     
  383.     PenNormal();
  384. }
  385.  
  386.